const StateManager = require("../template")

function testStateManagerBasic() {
  const stateManager = new StateManager({ count: 0, name: 'test' })

  console.log(
    "1. 基本状态获取测试:",
    (() => {
      const state = stateManager.getState()
      return state.count === 0 && state.name === 'test'
    })()
  )

  console.log(
    "2. 路径状态获取测试:",
    (() => {
      const count = stateManager.getState('count')
      const name = stateManager.getState('name')
      return count === 0 && name === 'test'
    })()
  )

  console.log(
    "3. 基本状态设置测试:",
    (() => {
      stateManager.setState('count', 5)
      const count = stateManager.getState('count')
      return count === 5
    })()
  )

  console.log(
    "4. 对象状态设置测试:",
    (() => {
      stateManager.setState({ count: 10, name: 'updated' })
      const state = stateManager.getState()
      return state.count === 10 && state.name === 'updated'
    })()
  )

  console.log(
    "5. 状态更新测试:",
    (() => {
      stateManager.updateState('count', (prev) => prev + 1)
      const count = stateManager.getState('count')
      return count === 11
    })()
  )

  console.log(
    "6. 状态订阅测试:",
    (() => {
      let notified = false
      stateManager.subscribe('count', () => {
        notified = true
      })
      stateManager.setState('count', 20)
      return notified === true
    })()
  )

  console.log(
    "7. 状态取消订阅测试:",
    (() => {
      let notified = false
      const callback = () => {
        notified = true
      }
      stateManager.subscribe('count', callback)
      stateManager.unsubscribe('count', callback)
      stateManager.setState('count', 30)
      return notified === false
    })()
  )

  console.log(
    "8. 批量更新测试:",
    (() => {
      let notifyCount = 0
      stateManager.subscribe('count', () => notifyCount++)
      stateManager.subscribe('name', () => notifyCount++)
      
      stateManager.batchUpdate({
        count: 40,
        name: 'batch'
      })
      
      return notifyCount === 2
    })()
  )

  console.log(
    "9. 状态重置测试:",
    (() => {
      stateManager.resetState({ count: 0, name: 'reset' })
      const state = stateManager.getState()
      return state.count === 0 && state.name === 'reset'
    })()
  )

  console.log(
    "10. 状态存在检查测试:",
    (() => {
      const hasCount = stateManager.hasState('count')
      const hasNonExistent = stateManager.hasState('nonExistent')
      return hasCount === true && hasNonExistent === false
    })()
  )

  console.log(
    "11. this上下文状态管理器测试:",
    (() => {
      const stateObj = {
        multiplier: 2,
        processState: function(value) {
          const sm = new StateManager({ value: value })
          return sm.getState('value') * this.multiplier
        }
      }
      return stateObj.processState(5) === 10
    })()
  )

  console.log(
    "12. 复杂对象this绑定状态管理器测试:",
    (() => {
      const stateConfigObj = {
        config: { threshold: 3 },
        processWithConfig: function(value) {
          const sm = new StateManager({ value: value })
          return sm.getState('value') >= this.config.threshold
        }
      }
      return stateConfigObj.processWithConfig(5) === true
    })()
  )
}

testStateManagerBasic()
